/*  
 * Rotate List
 * Given a List, rotate the list to the right by k places, where k is non-negtive
 * For example: Given 1->2->3->4->5->nullptr and k = 2
 * return 4->5->1->2->3->nullptr
 */

#include "../../func.h"

ListNode *rotateRight(ListNode *head, int k)
{
    if (nullptr == head || 0 == k)
        return head;

    int len = 1;
    ListNode *p = head;
    while (p->next)
    {
        len++;
        p = p->next;
    }
    k = len - k % len;

    p->next = head;
    for (int step = 0; step < k; ++step)
    {
        p = p->next;
    }
    head = p->next;
    p->next = nullptr;
    return head;
}